###########################################
#  使用方法: source nvm_mod.sh sn device
#  sn为jlink sn, device 为相应的芯片型号    
#
#
#  nvm参数修改文件为mod_nvm_file.txt
#  mod_nvm_file.txt 格式为 key : OBJ : DATA  
#
#
#
#
###########################################

SN=59401308
DEVICE_CHIP=EFR32FG23

OLD_NVM_FILE_S37=old_nvm_file.s37
MOD_NVM_FILE_TXT=mod_nvm_file.txt
NEW_NVM_FILE_S37=new_nvm_file.s37
NEW_NVM_FILE_R_S37=new_nvm_file_r.s37


if [ ! -n "$1" ]; then
	echo  "Jlink Use default:"$SN
else
	SN=$1
	echo "Jlink  use:"$SN
fi

if [ ! -n "$2" ]; then
	echo  "Device chip Use default:"$DEVICE_CHIP
else
	DEVICE_CHIP=$2
	echo "use device chip:"$DEVICE_CHIP
fi

rm -rf $OLD_NVM_FILE_S37
rm -rf $NEW_NVM_FILE_S37

# step 1: read nvm3 file
commander.exe  nvm3 read -o $OLD_NVM_FILE_S37  --device $DEVICE_CHIP  -s $SN
commander.exe  nvm3 parse $OLD_NVM_FILE_S37

# step 2: mod nvm3 file
commander.exe nvm3 set $OLD_NVM_FILE_S37  --nvm3file $MOD_NVM_FILE_TXT --outfile $NEW_NVM_FILE_S37
commander.exe nvm3 parse $NEW_NVM_FILE_S37

# step 3: write nvm3 file
echo "write nvm3 file"
commander.exe  flash $NEW_NVM_FILE_S37 --device $DEVICE_CHIP  -s $SN
commander.exe  device reset --device $DEVICE_CHIP  -s $SN

# [option] step 4: read nvm3 file

commander.exe  nvm3 read -o $NEW_NVM_FILE_R_S37 --device $DEVICE_CHIP  -s $SN
commander.exe  nvm3 parse $NEW_NVM_FILE_R_S37
commander.exe  device reset --device $DEVICE_CHIP  -s $SN

